﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Telerik.Windows.Controls;
using SaleLibrary.License;
using SaleLibrary.Tools;
using System.Net.Mail;
using BrokerBase.Lib.Tool;

namespace Broker.Mgr.SaleView
{
    /// <summary>
    /// Interaction logic for ActiveCodeSendForm.xaml
    /// </summary>
    public partial class ActiveCodeSendForm
    {
        private ActiveCodeSend send;
        private IList<SmtpClient> smtpClients;

        public ActiveCodeSendForm(ActiveCodeSend send):base()

        {
            this.send = send;
            InitializeComponent();
            Loaded += new RoutedEventHandler(ActiveCodeSendForm_Loaded);
        }

        void ActiveCodeSendForm_Loaded(object sender, RoutedEventArgs e)
        {
            LoadSmtps();

            DataContext = send;
        }

        private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            SendMail();
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void LoadSmtps()
        {
            try
            {
                smtpClients = MailSender.LoadSmtpClients(@"smtpclient.xml");
                cmbSendMail.ItemsSource = smtpClients;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        private void SendMail()
        {
            if (send.ActiveCode.Id == 0 || string.IsNullOrEmpty(send.ActiveCode.Code))
            {
                MessageBox.Show("请先保存,再发送邮件。");
                return;
            }

            if (!string.IsNullOrEmpty(send.Mail) && cmbSendMail.SelectedItem != null)
            {
                try
                {

                    MailAddress address = new MailAddress(send.Mail);
                    MessageBoxResult r = MessageBox.Show("是否要发送激活码到:" + send.Mail + "?", "提示", MessageBoxButton.YesNo);
                    if (r == MessageBoxResult.Yes)
                    {
                        SmtpClient smc = cmbSendMail.SelectedItem as SmtpClient;

                        MailMessage mail = new MailMessage();

                        try
                        {
                            mail.From = new MailAddress(((System.Net.NetworkCredential)smc.Credentials).UserName);
                            mail.To.Add(send.Mail);
                            mail.Subject = send.MailSubject;
                            mail.Body = send.MailBody;
                            mail.BodyEncoding = System.Text.Encoding.UTF8;
                            smc.Send(mail);

                            

                            send.ActiveCode.Sends.Add(send);
                            send.ActiveCode.SendTime = DateTime.Now;
                            send.SendTime = DateTime.Now;

                            MessageBox.Show("邮件发送成功!", "提示");


                            try
                            {
                                DBServiceUtil.GetService<ActiveCodeSend>(AppGlobal.HbmSessionFactory.GetCurrentSession()).SaveOrUpdate(send);
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
                        }
                        catch (Exception er)
                        {
                            MessageBox.Show(er.Message);
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("邮件地址:" + send.Mail + "不合法，请重新输入!");
                }
            }
            else if (string.IsNullOrEmpty(send.Mail))
            {
                MessageBox.Show("请输入接受邮件地址!");
            }
            else if (cmbSendMail.SelectedItem == null)
            {
                MessageBox.Show("请选择发送邮件地址!");
            }
        }

    }
}
